package com.hwn.datax.controller;

import com.hwn.datax.component.SpringFXMLLoader;
import com.hwn.datax.component.StatusDisplay;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.transform.Scale;
import javafx.stage.Modality;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author wnhuang
 * @date 2024/5/30 16:17
 */
@Slf4j
@Controller
public class NavigationController implements Initializable {


    @FXML
    public BorderPane root;

    @Autowired
    private SpringFXMLLoader springFXMLLoader;

    @FXML
    public VBox navBar;


    @FXML
    private void showPage1() {
        loadPage("views/job-run.fxml");
    }

    @FXML
    private void showPage2() {

        // 使用StackPane自动居中
        StackPane stackPane = new StackPane();
        StatusDisplay statusDisplay = new StatusDisplay();
        statusDisplay.statusProperty().set("loading");
        stackPane.getChildren().add(statusDisplay);
        stackPane.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)
        ));

        // 设置Pane的大小，确保SVG图形始终处于中心位置
        stackPane.setPrefSize(200, 200);
        // 创建Scale变换对象
        Scale scaleTransform = new Scale();
        scaleTransform.setX(0.6); // 缩放比例
        scaleTransform.setY(0.6);
        scaleTransform.setPivotX(0); // 设定缩放原点为左上角
        scaleTransform.setPivotY(0);

        // 将Scale变换添加到SVGPath
        statusDisplay.getTransforms().add(scaleTransform);

        StatusDisplay statusDisplay1 = new StatusDisplay();
        statusDisplay1.statusProperty().set("loading");
        stackPane.getChildren().add(statusDisplay1);

        Scene scene = new Scene(stackPane);

        Stage stage = new Stage();
        stage.setScene(scene);
        // Make the popup modal
        stage.initModality(Modality.APPLICATION_MODAL);
        // Show the popup and wait until it is closed
        stage.showAndWait();
    }

    @FXML
    private void showPage3() {
        loadPage("views/job-edit.fxml");
    }

    @FXML
    private void showPage4() {
        showPopup("views/repo-config.fxml","驱动配置");
    }

    @FXML
    private void showPage5() {
        showPopup("views/config.fxml","系统配置");
    }

    private void loadPage(String fxmlFile) {
        root.setCenter(springFXMLLoader.load(fxmlFile));
    }

    @FXML
    public void addRepo(ActionEvent actionEvent) {
        showPopup("views/ConnectionForm.fxml", "编辑数据源");
    }

    private void showPopup(String fxmlFile, String title) {
        Parent root = springFXMLLoader.load(fxmlFile);
        Stage stage = new Stage();
        stage.setTitle(title);
        stage.setScene(new Scene(root));
        // Make the popup modal
        stage.initModality(Modality.APPLICATION_MODAL);
        // Show the popup and wait until it is closed
        stage.showAndWait();
    }

    public void showPage6(ActionEvent actionEvent) {
        showPopup("views/server-config.fxml", "编辑服务器");
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        showPage1();
    }
}
